package cn.wsalix.admin.entity;

import java.util.Date;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.DiscriminatorColumn;
import javax.persistence.DiscriminatorType;
import javax.persistence.DiscriminatorValue;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.ForeignKey;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToOne;
import javax.persistence.PrimaryKeyJoinColumn;
import javax.persistence.Table;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;

import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.ToString;

import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;
import org.hibernate.annotations.DynamicInsert;
import org.hibernate.annotations.DynamicUpdate;

import cn.anlaser.entity.CommonEntity;
import cn.wsalix.admin.status.UserEnum;
import cn.wsalix.status.PlatformEnum;
import cn.wsalix.type.UserRegTypeEnum;
import cn.wsalix.user.entity.UserCenter;
import cn.wsalix.user.entity.UserInfo;

@Entity
@Table(name = "ADMIN_USER")
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
@DynamicUpdate
@DynamicInsert
@Data
@EqualsAndHashCode(exclude = { "password", "salt", "userInfo", "userCenter" }, callSuper = false)
@ToString(exclude = { "password", "salt", "userInfo", "userCenter" })
@DiscriminatorColumn(name = "user_type", discriminatorType = DiscriminatorType.STRING, length = 15)
@DiscriminatorValue("ADMIN")
public class SysUser extends CommonEntity {

	/**
	 * 
	 */
	private static final long serialVersionUID = -8235355705282930022L;

	@NotNull(message = "{message.username.empty}")
	@Size(min = 2, max = 140, message = "{message.username.length}")
	@Column(name = "USER_NAME", nullable = false, length = 20)
	private String username;
	private String realname;
	private String avatar;
	@Column(name = "SALT")
	private String salt;
	@Column(name = "PASSWORD", nullable = false)
	private String password;// 密码
	// private UserTypeEnum userType;// 用户类型 使用系统用户/非使用系统用户
	private UserRegTypeEnum regType;
	private String token;
	/*
	 * @ManyToOne(cascade = CascadeType.MERGE, optional = true, fetch =
	 * FetchType.LAZY)
	 * 
	 * @JoinColumn(foreignKey = @ForeignKey(name = "FK_USER_STATUS"), name =
	 * "STATUS_ID") private SysDict status;
	 */
	private UserEnum status;
	
	// private Boolean locked = Boolean.FALSE;
	private PlatformEnum platform;
	private Date lastLoginTime;
	private boolean onlineFlag;

	@OneToOne(optional = true, cascade = CascadeType.MERGE, fetch = FetchType.EAGER)
	@PrimaryKeyJoinColumn(name = "ID", referencedColumnName = "ID")
	private UserInfo userInfo;

	@OneToOne(optional = true, cascade = CascadeType.MERGE, fetch = FetchType.EAGER)
	@PrimaryKeyJoinColumn(name = "ID", referencedColumnName = "ID")
	private UserCenter userCenter;

	@ManyToOne(cascade = CascadeType.MERGE, optional = true, fetch = FetchType.EAGER)
	@JoinColumn(foreignKey = @ForeignKey(name = "FK_USER_ROLE"), name = "ROLE_ID")
	private SysRole role;

	public String getCredentialsSalt() {
		return username + salt;
	}

	public SysUser(String username, String password) {
		this.username = username;
		this.password = password;
	}

	public SysUser() {
		// TODO Auto-generated constructor stub
	}

}